최영범
07/21/2025, 1:10 PMMarvin
07/21/2025, 1:10 PMMarvin
07/21/2025, 1:11 PMpython
{
"image": "your-docker-image:tag",
"cpu": 1,
"memory": 512,
"memory_unit": "Mi",
"region": "us-central1"
}
You can set this either through the Prefect UI or using the CLI:
bash
prefect work-pool create "my-cloud-run-pool" \
--type cloud-run \
--base-job-template path/to/template.json
### Option 2: Setting the image in the Deployment
When creating a deployment, you can specify the image in the deployment configuration:
python
from prefect import flow
from prefect.deployments import Deployment
@flow
def my_flow():
# your flow code here
pass
deployment = Deployment.build_from_flow(
flow=my_flow,
name="my-deployment",
work_pool_name="my-cloud-run-pool",
job_variables={
"image": "your-docker-image:tag"
}
)
Or when using the modern deployment syntax in Prefect 3.x:
python
@flow
def my_flow():
# your flow code here
pass
if __name__ == "__main__":
my_flow.deploy(
name="my-deployment",
work_pool_name="my-cloud-run-pool",
job_variables={
"image": "your-docker-image:tag"
}
)
Important notes:
1. The image must be accessible to your GCP Cloud Run service
2. If you're using a private container registry, make sure your Cloud Run service has the necessary permissions to pull the image
3. The image should contain all the dependencies required by your flow
4. Make sure the image has Python and the required Prefect version installed
Would you like me to provide more specific details about any of these configuration options or help with setting up a particular approach?최영범
07/21/2025, 1:17 PMMarvin
07/21/2025, 1:17 PMMarvin
07/21/2025, 1:18 PMasia-northeast3
- Make sure your service account has permissions in this region
- Verify the region matches what's configured in your work pool
Would you like me to help you verify any specific part of this setup? Please let me know:
1. Which service account you're using
2. Whether you're using Workload Identity
3. The specific GCP project ID
This will help me provide more targeted assistance.최영범
07/21/2025, 1:21 PMroles/run.admin
Marvin
07/21/2025, 1:21 PMMarvin
07/21/2025, 1:22 PMMarvin
07/21/2025, 1:24 PMMarvin
07/21/2025, 1:24 PMbash
gcloud run services describe YOUR_WORKER_SERVICE_NAME \
--region asia-northeast3 \
--format="get(serviceConfig.serviceAccountEmail)"
2. This service account needs the following permissions:
- roles/run.developer
- to create and manage Cloud Run jobs
- roles/run.invoker
- to invoke Cloud Run services
- roles/iam.serviceAccountUser
- to act as the service account
You can grant these permissions using:
bash
# Replace YOUR_PROJECT_ID and SERVICE_ACCOUNT_EMAIL with actual values
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/run.developer"
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/run.invoker"
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/iam.serviceAccountUser"
3. Additionally, check if the Cloud Run API is enabled:
bash
gcloud services enable <http://run.googleapis.com|run.googleapis.com>
4. Verify region permissions:
- The error shows you're using asia-northeast3
- Make sure your service account has permissions in this region
- Confirm this matches your work pool configuration
To help you further, could you please:
1. Confirm if you're using the default compute service account (<mailto:PROJECT_NUMBER-compute@developer.gserviceaccount.com|PROJECT_NUMBER-compute@developer.gserviceaccount.com>
) or a custom service account?
2. Can you check if the Cloud Run API is enabled in your project?
3. Are you able to create Cloud Run jobs manually in the GCP Console with the same service account?
This will help me provide more specific guidance for your setup.
Also, as a temporary test, you could try granting the more permissive roles/run.admin
role to verify if it's purely a permissions issue:
bash
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/run.admin"
Note: While this would work for testing, it's recommended to use the more granular permissions mentioned above for production use.최영범
07/22/2025, 2:05 AMresource "google_cloud_run_v2_service" "prefect_worker" {
project = var.project
name = "prefect-worker"
location = var.location
deletion_protection = false
depends_on = [google_cloud_run_v2_service.prefect_server]
template {
scaling {
max_instance_count = 1
min_instance_count = 1
}
containers {
image = local.prefect_server_image
command = ["/bin/sh", "-c"]
args = [
<<-EOT
prefect worker start --install-policy always --with-healthcheck -p cloud_run_work_pool -t cloud-run
EOT
]
env {
name = "PREFECT_API_URL"
value = local.prefect_api_url
}
service_account = var.prefect_server_service_account_email
And the prefect server service account has roles below:
• Cloud Run Admin
• Cloud SQL Client
• IAP-secured Web App User
• Service Account User
And the worker still fail to create job.Marvin
07/22/2025, 2:05 AMMarvin
07/22/2025, 2:05 AMCloud Run Admin
and Service Account User
roles, there might be a specific requirement for Cloud Run job creation. Let's modify the service account permissions to ensure it has all necessary permissions.
Given your Terraform configuration, the service account needs additional permissions to create and manage Cloud Run jobs. Here's what you should add to your service account:
1. Add IAM permissions for job creation. You can add this to your Terraform configuration:
hcl
resource "google_project_iam_member" "prefect_server_cloudrun_jobs" {
project = var.project
role = "roles/run.developer"
member = "serviceAccount:${var.prefect_server_service_account_email}"
}
# If not already present, add this for job invocation
resource "google_project_iam_member" "prefect_server_cloudrun_invoker" {
project = var.project
role = "roles/run.invoker"
member = "serviceAccount:${var.prefect_server_service_account_email}"
}
2. Verify the service account has permission to create other service accounts (needed for job execution):
hcl
resource "google_project_iam_member" "prefect_server_sa_creator" {
project = var.project
role = "roles/iam.serviceAccountCreator"
member = "serviceAccount:${var.prefect_server_service_account_email}"
}
3. Add permission to act as service accounts:
hcl
resource "google_project_iam_member" "prefect_server_sa_user" {
project = var.project
role = "roles/iam.serviceAccountUser"
member = "serviceAccount:${var.prefect_server_service_account_email}"
}
4. Additionally, make sure your Cloud Run service has the necessary security context:
hcl
resource "google_cloud_run_v2_service" "prefect_worker" {
# ... your existing configuration ...
template {
containers {
# ... your existing container configuration ...
# Add this if not present
service_account = var.prefect_server_service_account_email
}
}
}
5. Enable the necessary APIs if not already enabled:
hcl
resource "google_project_service" "required_apis" {
for_each = toset([
"<http://run.googleapis.com|run.googleapis.com>",
"<http://iam.googleapis.com|iam.googleapis.com>",
"<http://cloudresourcemanager.googleapis.com|cloudresourcemanager.googleapis.com>"
])
project = var.project
service = each.value
disable_on_destroy = false
}
After applying these changes:
1. Apply the Terraform changes
2. Restart your Cloud Run worker service
3. Try running a deployment again
If you're still seeing issues, could you please:
1. Check the Cloud Run service logs for any additional error messages
2. Verify if the service account has the correct project-level permissions by running:
bash
gcloud projects get-iam-policy YOUR_PROJECT_ID \
--flatten="bindings[].members" \
--format='table(bindings.role)' \
--filter="bindings.members:${var.prefect_server_service_account_email}"
This will show all roles assigned to your service account at the project level.
Would you like me to provide any additional details about any of these configurations or help troubleshoot further if these changes don't resolve the issue?최영범
07/22/2025, 2:11 AMMarvin
07/22/2025, 2:11 AMMarvin
07/22/2025, 2:12 AMroles/iam.serviceAccountCreator
role. After researching further, I can confirm that this role is not required for the Cloud Run worker to function properly.
For a Cloud Run worker to create and manage Cloud Run jobs, you only need these essential roles:
1. roles/run.developer
- to create and manage Cloud Run jobs
2. roles/run.invoker
- to invoke Cloud Run services
3. roles/iam.serviceAccountUser
- to act as the service account
Your current service account already has sufficient permissions with:
- Cloud Run Admin (which is actually more permissive than needed)
- Service Account User
- Cloud SQL Client (for database access)
- IAP-secured Web App User
The serviceAccountCreator
role is not necessary because:
1. The Cloud Run worker doesn't need to create new service accounts
2. It only needs to execute Cloud Run jobs using the existing service account
Would you like me to provide a more minimal set of IAM permissions that would work for your use case? This could help improve security by following the principle of least privilege.최영범
07/22/2025, 6:11 AMFailed to submit flow run '311c9d1b-8672-48b6-97fd-716b76a1b67e' to infrastructure.
Traceback (most recent call last):
File "/usr/local/lib/python3.13/site-packages/prefect/workers/base.py", line 1258, in _submit_run_and_capture_errors
configuration = await self._get_configuration(flow_run)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/prefect/workers/base.py", line 1359, in _get_configuration
configuration = await self.job_configuration.from_template_and_values(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<3 lines>...
)
^
File "/usr/local/lib/python3.13/site-packages/prefect/client/utilities.py", line 99, in with_injected_client
return await fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/prefect/workers/base.py", line 199, in from_template_and_values
return cls(**populated_configuration)
File "/usr/local/lib/python3.13/site-packages/pydantic/main.py", line 253, in __init__
validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for CloudRunWorkerJobV2Configuration
job_body
Value error, Job body is missing required components: /template [type=value_error, input_value={'kind': 'Job', 'spec': {...'<http://run.googleapis.com/v1|run.googleapis.com/v1>'}, input_type=dict]
For further information visit <https://errors.pydantic.dev/2.11/v/value_error>
Marvin
07/22/2025, 6:11 AMMarvin
07/22/2025, 6:12 AMjson
{
"job": {
"template": {
"template": {
"containers": [
{
"image": "${image}",
"resources": {
"cpu_limit": "1",
"memory_limit": "512Mi"
},
"env": [
{
"name": "PREFECT_API_URL",
"value": "${prefect_api_url}"
},
{
"name": "PREFECT_API_KEY",
"value": "${prefect_api_key}"
}
]
}
],
"serviceAccount": "${service_account_name}",
"timeoutSeconds": "600"
}
}
}
}
You can update your work pool in one of two ways:
1. Using the UI:
- Go to your work pool settings
- Click "Edit"
- In the "Base Job Template" section, paste the above JSON
- Update the values as needed
2. Using the CLI:
bash
# First save the template to a file named template.json
prefect work-pool create "your-pool-name" \
--type cloud-run-v2 \
--base-job-template template.json
Key points about the template:
1. Note the nested template
structure - this is required for Cloud Run V2
2. The outer job
wrapper is required
3. Variables in ${...}
will be replaced with actual values at runtime
4. Make sure to set appropriate resource limits for your needs
Variables you can use in the template:
- `${image}`: Docker image to use
- `${prefect_api_url}`: Your Prefect API URL
- `${prefect_api_key}`: API key if needed
- `${service_account_name}`: The service account email
Would you like me to:
1. Provide a more detailed template with additional configuration options?
2. Help you troubleshoot the specific values in your current configuration?
3. Show how to verify the configuration is correct before deploying?Marvin
07/22/2025, 6:50 AM최영범
07/22/2025, 7:00 AMMarvin
07/22/2025, 7:00 AMMarvin
07/22/2025, 7:00 AMjson
{
"template": {
"containers": [
{
"image": "${image}",
"resources": {
"limits": {
"cpu": "1",
"memory": "512Mi"
}
},
"env": [
{
"name": "PREFECT_API_URL",
"value": "${prefect_api_url}"
},
{
"name": "PREFECT_API_KEY",
"value": "${prefect_api_key}"
}
]
}
],
"serviceAccount": "${service_account_name}",
"timeoutSeconds": 600
}
}
Key differences in this V2 template:
1. The structure is flatter - no nested job
wrapper
2. Resources are specified under limits
3. The template is directly at the root level
You can update your work pool using one of these methods:
1. Using the UI:
- Go to your work pool settings
- Click "Edit"
- Replace the entire base job template with the above JSON
- Save the changes
2. Using the CLI:
bash
# Save the template to a file
echo '{
"template": {
"containers": [
{
"image": "${image}",
"resources": {
"limits": {
"cpu": "1",
"memory": "512Mi"
}
},
"env": [
{
"name": "PREFECT_API_URL",
"value": "${prefect_api_url}"
},
{
"name": "PREFECT_API_KEY",
"value": "${prefect_api_key}"
}
]
}
],
"serviceAccount": "${service_account_name}",
"timeoutSeconds": 600
}
}' > template.json
prefect work-pool create "your-pool-name" \
--type cloud-run-v2 \
--base-job-template template.json
Optional additional configurations you can add:
json
{
"template": {
"containers": [
{
// ... existing configuration ...
"args": ["${command}"], // If you need to pass specific commands
"labels": { // If you need labels
"<http://prefect.io/flow-run-id|prefect.io/flow-run-id>": "${flow_run_id}"
}
}
],
"vpc": { // If you need VPC access
"connector": "your-vpc-connector",
"egress": "ALL_TRAFFIC"
}
}
}
Would you like me to:
1. Help you troubleshoot if this template still doesn't work?
2. Explain any specific part of the configuration in more detail?
3. Show how to add additional configurations for your specific needs?최영범
07/22/2025, 8:00 AMMarvin
07/22/2025, 8:00 AMMarvin
07/22/2025, 8:01 AMbash
prefect block register -m prefect_gcp
b. Then create the credentials block either through the UI or Python:
python
from prefect_gcp import GcpCredentials
credentials = GcpCredentials(
service_account_info=None, # Leave as None when running on GCP
service_account_file=None, # Leave as None when running on GCP
project="your-project-id" # Your GCP project ID
)
credentials.save("gcp-credentials-block-name")
2. Worker Service Account:
Since your worker is running as a Cloud Run service, it should use the default compute service account or a custom service account. This service account needs:
bash
# Check the current service account being used by your worker
gcloud run services describe prefect-worker \
--region YOUR_REGION \
--format="get(serviceConfig.serviceAccountEmail)"
Ensure this service account has these roles:
bash
# Replace SERVICE_ACCOUNT_EMAIL with the email from above command
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/run.developer"
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/run.invoker"
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
--role="roles/iam.serviceAccountUser"
3. Update Work Pool Configuration:
Make sure your work pool has the credentials block configured. In the work pool settings, you should see a field for GCP Credentials. Make sure it's set to your credentials block.
You can also update it via CLI:
bash
prefect work-pool update "your-pool-name" \
--set variables='{"credentials": "gcp-credentials-block-name"}'
To troubleshoot:
1. Check Worker Logs:
bash
gcloud run services logs read prefect-worker --region YOUR_REGION
Look for more detailed error messages about the credentials issue.
2. Verify Work Pool Configuration:
bash
prefect work-pool inspect "your-pool-name"
Check that the credentials block is properly referenced.
3. Test Service Account Permissions:
bash
# Get the service account currently being used
SA_EMAIL=$(gcloud run services describe prefect-worker \
--region YOUR_REGION \
--format="get(serviceConfig.serviceAccountEmail)")
# List its roles
gcloud projects get-iam-policy YOUR_PROJECT_ID \
--flatten="bindings[].members" \
--format='table(bindings.role)' \
--filter="bindings.members:$SA_EMAIL"
Could you please:
1. Check if you see the credentials block properly configured in your work pool settings?
2. Share any specific error message you're seeing about the missing credentials?
3. Confirm whether you're using a custom service account or the default compute service account?
This will help me provide more targeted assistance for your setup.